package com.example.admin.calculater;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    Button num1Btn;
    Button num2Btn;
    Button num3Btn;

    Button num4Btn;
    Button num5Btn;
    Button num6Btn;

    Button num7Btn;
    Button num8Btn;
    Button num9Btn;

    Button clsBtn;
    Button zeroBtn;
    Button dotBtn;

    //function Button
    Button addBtn;
    Button subBtn;
    Button mulBtn;
    Button deviBtn;
    Button equalBtn;

    EditText calcuText;

    ListView listView;

    int temp;

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        calcuText=(EditText)findViewById(R.id.calcuText);

        num1Btn=(Button)findViewById(R.id.num1Btn);
        num2Btn=(Button)findViewById(R.id.num2Btn);
        num3Btn=(Button)findViewById(R.id.num3Btn);
        num4Btn=(Button)findViewById(R.id.num4Btn);
        num5Btn=(Button)findViewById(R.id.num5Btn);
        num6Btn=(Button)findViewById(R.id.num6Btn);
        num7Btn=(Button)findViewById(R.id.num7Btn);
        num8Btn=(Button)findViewById(R.id.num8Btn);
        num9Btn=(Button)findViewById(R.id.num9Btn);
        zeroBtn=(Button)findViewById(R.id.zeroBtn);
        dotBtn=(Button)findViewById(R.id.dotBtn);

        addBtn=(Button)findViewById(R.id.addBtn);
        subBtn=(Button)findViewById(R.id.subBtn);
        mulBtn=(Button)findViewById(R.id.mulBtn);
        deviBtn=(Button)findViewById(R.id.deviBtn);
        equalBtn=(Button)findViewById(R.id.equalBtn);

        clsBtn=(Button)findViewById(R.id.clsBtn);

        listView=(ListView)findViewById(R.id.MyListView);



//        set Button ClickListener
        num1Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"Hello",Toast.LENGTH_SHORT).show();
                String str=calcuText.getText().toString();
                str+="1";
                calcuText.setText(str);

            }
        });

        num2Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="2";
                calcuText.setText(str);
            }
        });

        num3Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="3";
                calcuText.setText(str);

            }
        });

        num4Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="4";
                calcuText.setText(str);
            }
        });

        num5Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="5";
                calcuText.setText(str);
            }
        });

        num6Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="6";
                calcuText.setText(str);
            }
        });

        num7Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="7";
                calcuText.setText(str);
            }
        });

        num8Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="8";
                calcuText.setText(str);
            }
        });

        num9Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="9";
                calcuText.setText(str);
            }
        });

        zeroBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+="0";
                calcuText.setText(str);
            }
        });

        dotBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=calcuText.getText().toString();
                str+=".";
                calcuText.setText(str);
            }
        });

        clsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                calcuText.setText("");
            }
        });


        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                temp=Integer.parseInt(calcuText.getText().toString());
                calcuText.setText("");

            }
        });

        equalBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int sum=temp+Integer.parseInt(calcuText.getText().toString());
                String str=""+sum;

                SharedPreferences user=getSharedPreferences("record",0);

                SharedPreferences.Editor edit=user.edit();

                edit.putString("record",str);

                edit.commit();

                SharedPreferences read=getSharedPreferences("record",0);

                String str1=read.getString("record","");

                Toast.makeText(MainActivity.this,str1,Toast.LENGTH_SHORT).show();


                calcuText.setText(str);

                SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日    HH:mm:ss");
                Date curDate = new Date(System.currentTimeMillis());
                String time = formatter.format(curDate);

                System.out.println(time);

                Toast.makeText(MainActivity.this,time,Toast.LENGTH_SHORT).show();


                db=SQLiteDatabase.openOrCreateDatabase("/data/com.example.admin.calculater/database/contentList.db",null);
//
//                String con_table="create table con_table(time text,content text)";
//
//                db.execSQL(con_table);
//
//                ContentValues cValue=new ContentValues();
//                cValue.put("time",time);
//                cValue.put("content",str1);
//
//                db.insert(con_table,null,cValue);
//
//                Cursor cursor=db.query("con_table",null,null,null,null,null,null);

//                if (cursor.moveToFirst())
//                {
//                    for (int i=0; i<cursor.getCount();i++)
//                    {
//                        cursor.move(i);
//
//                        int id=cursor.getInt(0);
//
//                        String timeOfput=cursor.getString(1);
//
//                        String textOfput=cursor.getString(2);
//
//
//                        System.out.println(id+":"+timeOfput+":"+textOfput);
//                    }
//                }

            }
        });


        //生成动态数组，并且转载数据
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        for(int i=0;i<30;i++)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "This is Title.....");
            map.put("ItemText", "This is text.....");
            mylist.add(map);
        }
        //生成适配器，数组===》ListItem
        SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释
                mylist,//数据来源
                R.layout.my_listitem,//ListItem的XML实现

                //动态数组与ListItem对应的子项
                new String[] {"ItemTitle", "ItemText"},

                //ListItem的XML文件里面的两个TextView ID
                new int[] {R.id.ItemTitle,R.id.ItemText});
        //添加并且显示
        listView.setAdapter(mSchedule);


    }




}
